package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo09 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        System.out.println("请输入昵称");
        String nick = sc.nextLine();

        try ( Connection conn = DBUtils.getConn()){
            String sql="select username from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1,username);

            ResultSet resultSet = ps.executeQuery();

            if (resultSet.next()) {
                System.out.println("用户已存在!");
                return;
            }else{
                    String sql2="insert into user values(null,?,?,?)";
                    PreparedStatement ps2 = conn.prepareStatement(sql2);
                    ps2.setString(1,username);
                    ps2.setString(2,password);
                    ps2.setString(3,nick);

                    ps2.executeUpdate();
                    System.out.println("注册成功!");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
